/*
-------------------------------------------------------------------
BIE is Copyright 2001-2003 Brunswick Corp.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/bie_license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------
*/
package com.webdeninteractive.sbie.service;

import com.webdeninteractive.sbie.Service;
import com.webdeninteractive.sbie.Client;
import com.webdeninteractive.sbie.ProtocolHandler;
import com.webdeninteractive.sbie.config.SystemConfig;

import java.util.Map;
import java.util.Hashtable;
import java.util.Iterator;

/** 
 * Basic implementation of standard Service methods.
 * <p>
 * This is the basic getters and setters.  A class extending this
 * needs only implement <code>runService</code>.
 *
 * @author gfast
 * @version $Id: ServiceBase.java,v 1.1.1.1 2003/05/20 16:56:49 gdf Exp $ 
 */
abstract public class ServiceBase implements Service {
    
    protected ProtocolHandler protocolHandler;
    protected Client parent;
    protected Hashtable params = new Hashtable();
    protected SystemConfig config;

    public void setProtocolHandler( ProtocolHandler handler ) {
        this.protocolHandler = handler;
    }

    public ProtocolHandler getProtocolHandler() {
        return this.protocolHandler;
    }

    public void setParentClient( Client parent ) {
        this.parent = parent;
    }

    public Client getParentClient() {
        return this.parent;
    }

    // setter only: internal
    public void setConfiguration( SystemConfig config ) {
        this.config = config;
    }

    public SystemConfig getConfiguration() {
        return this.config;
    }

    // Parameters are set by the client, per configuration
    public void setParameter( String param, String value ) {
        params.put(param, value);
    }        

    public String getParameter(String param) {
        return (String)params.get(param);
    }

    public String removeParameter(String param) {
        return (String)params.remove(param);
    }

    /** This returns live references: altering this Map will 
     *  alter the parameters of this Service (but don't *do* that!).
     */
    public Map getParameters() {
        return (Map)params; // potentially bad, but copies are slow...
    }

    public void setParameters( Map parameters ) {
        params = new Hashtable();
        Iterator keys = parameters.keySet().iterator();
        while( keys.hasNext() ) {
            Object k = keys.next();
            params.put(k, parameters.get(k));
        }
    }

}
